<style lang="less" scoped>

  .el-row {
    margin-bottom: 20px;
  }

  .el-col {
    border-radius: 4px;
  }

  .bg-purple-dark {
    background: #ffffff;
  }

  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }

  .el-form-item {
    margin: 0px 20px 15px 20px;
  }

  .f-bs1 {
    border: 1px solid #dfe6ec;
  }

  .el-collapse {
    border: 0px;
    .el-collapse-item {
      border: 1px solid #dfe6ec;
      margin-bottom: 10px;
      .el-collapse-item__header {
        font-weight: bold;
        .el-collapse-item__header__arrow {
          display: none;
        }
      }
      .el-collapse-item__content {
        padding-bottom: 0px;
      }
      .el-collapse-item__wrap {
        border-bottom: 0px;
      }
    }
  }

  label {
    width: 100px;
  }

  .el-form-item[data-v-7da61f17] {
    margin: 0 0 0 0
  }

  .f-mt10 {
    margin-top: 10px;
  }

  .f-ml20 {
    margin-left: 20px;
  }

  .f-mr20 {
    margin-right: 20px;
  }

  .f-mb10 {
    margin-bottom: 10px;
  }

  .f-tar {
    text-align: right;
  }

  .f-h30 {
    height: 30px;
  }

  .f-c-red {
    color: red;
  }

  .el-radio-group {
    width: 60%;
  }

  .el-radio-invoice {
    margin-right: 30px;
  }

  .f-color {
    color: #25baaf;
  }

  .el-textarea {
    width: 86%;
  }

  .f-mb {
    margin-bottom: 0;
  }

  .f-fr {
    float: right;
  }

  .f-fl {
    float: left;
  }

  .f-lh38 {
    line-height: 38px;
  }

  .el-select {
    width: 100%;
  }

  .f-w233 {
    width: 100%;
  }

  .el-form-item__label {
    width: 150px;
  }

  .el-date-editor.el-input {
    width: 100%;
  }

  li {
    list-style-type: none;
  }

  .color-c8c8c8 {
    color: #c8c8c8;
  }

  .f-mt {
    margin-top: 0;
  }

  .width70 {
    width: 82%;
  }

</style>

<template>
  <section>
    <!--工具条-->

    <!-- 操作 -->
    <el-col :span="24" style="margin-bottom: 10px">
      <el-button type="primary" @click="handleAdd">新增</el-button>
    </el-col>

    <!--列表-->
    <el-table :data="tableData" highlight-current-row border @sort-change="sortChange"
              @selection-change="selsChange" v-loading="listLoading" style="width: 100%;">
      <el-table-column type="index" label="序号" width="100">
      </el-table-column>
      <el-table-column prop="dictValueObj.cnDictValname" label="计费条目" width="200" >
      </el-table-column>
      <el-table-column prop="value" label="比例/金额(元)" width="200" >
      </el-table-column>
      <el-table-column prop="billingMode" label="计费方式" width="300" >
      </el-table-column>
      <el-table-column prop="remark" label="备注" >
      </el-table-column>

      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button  type="primary" @click="getEditBYId(scope.$index, scope.row)">编辑 </el-button>
          <el-button type="danger"  @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination
        :current-page="currentPage"
        :page-size="pageSize"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total,sizes,prev, pager, next, jumper"
        :total="total" style="float: right">
      </el-pagination>
    </el-col>
    <!--新增 弹窗 -->
    <el-dialog :title="dialogTitle" size="tiny" :visible.sync="addValueVisible" :close-on-click-modal="false">
      <el-form :model="addValue" :rules="rules" label-width="110px" ref="addValue">
        <el-row class="f-mt10 f-mb10">
          <el-col :span="24">
            <el-form-item label="计费条目" class="width70" prop="dictValue">
              <el-select  v-model="addValue.dictValue"
                         placeholder="请选择">
                <el-option
                  v-for="item in $store.getters.VALUE_ADDED_SERVICES"
                  :key="item.dictVal"
                  :label="item.cnDictValname"
                  :value="item.dictVal">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row class="f-mt10 f-mb10">
          <el-col :span="24">
            <el-form-item label="比例/金额(元)" class="width70" prop="value">
              <el-input type="number" v-model.number="addValue.value" placeholder="请输入比例/金额"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row class="f-mt10 f-mb10">
          <el-col :span="24">
            <el-form-item label="计费方式" class="width70" prop="billingMode">
              <el-input v-model="addValue.billingMode" placeholder="请输入计费方式"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row class="f-mt10 f-mb10">
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input type="textarea" v-model="addValue.remark" placeholder="请输入计费方式"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addValueVisible = false">取消</el-button>
        <el-button type="primary" @click.native="handleSubmit" :loading="submitLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
import {
  searchAddValue,
  saveAddValue,
  delAddValue
} from '../../api/freight/addValue';
export default {
  data () {
    return {
      dictValues: [],

      dialogTitle: '新增',
      condition: {
        name: '',
        number: ''
      },

      sels: [],
      tableData: [],
      total: 0,
      currentPage: 1,
      pageSize: 10,
      sortProp: null,
      sortOrder: 'ascending',
      listLoading: false,
      addValueVisible: false,
      submitLoading: false,

      // 表单数据
      addValue: {
        'id': null,
        'dictValue': [],
        'dictValueObj': null,
        'value': null,
        'billingMode': null,
        'remark': null

      },
      // 验证规则
      rules: {
        dictValue: [
          {required: true, type: 'number', message: '请输入比例/金额', trigger: 'blur'}
        ],
        value: [
          {type: 'number', required: true, message: '请输入比例/金额', trigger: 'blur'}
        ],
        billingMode: [
          {required: true, message: '请输入计费方式', trigger: 'blur'},
          {min: 2, max: 60, message: '长度在2到 60 个字符', trigger: 'blur'}
        ]
      }
    };
  },

  methods: {
    // 删除单条数据操作
    handleDel (index, row) {
      this.$confirm('确认删除该增值服务吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        delAddValue(row.id).then((res) => {
          this.listLoading = false;
          this.$message({
            message: '删除成功！',
            type: 'success'
          });
          this.getTableData();
        });
      }).catch(() => {
      });
    },

    // 打开编辑界面
    getEditBYId (index, row) {
      this.resetForm();
      this.addValue = Object.assign({}, row);
      this.dialogTitle = '修改';
      this.addValueVisible = true;
    },

    // 弹出添加窗体
    handleAdd () {
      this.dialogTitle = '新增';
      this.resetForm();
      this.addValueVisible = true;
    },
    // 重置表单
    resetForm () {
      if (this.$refs.addValue) {
        this.$refs.addValue.resetFields();
      }
      // 当先点击修改，再点击新增，会出现值没有清空,修复处理
      for (let p in this.addValue) {
        if (this.addValue[p] instanceof Array) {
          this.addValue[p] = [];
        } else {
          this.addValue[p] = null;
        }
      }
    },

    // 多选操作
    selsChange (sels) {
      this.sels = sels;
    },
    // 排序操作
    sortChange (o) {
      this.sortProp = o.prop;
      this.sortOrder = o.order;
      this.getTableData();
    },
    // 每页几条操作
    handleSizeChange (val) {
      this.pageSize = val;
      this.getTableData();
    },
    // 第几页操作
    handleCurrentChange (val) {
      this.currentPage = val;
      this.getTableData();
    },
    // 新增数据提交
    handleSubmit () {
      // 表单验证
      this.$refs['addValue'].validate((valid) => {
        if (valid) {
          this.submitLoading = true;
          saveAddValue(this.addValue).then((res) => {
            this.submitLoading = false;
            if (res.data.successFlg === true) {
              this.addValueVisible = false;
              this.getTableData();
              this.$message.success('保存/修改成功！');
            } else {
              this.$message.error(res.data.errorMsg);
            }
          }).catch(function (error) {
            console.log(error);
          });
        } else {
          return false;
        }
      });
    },

    // 获取表格数据
    getTableData () {
      this.listLoading = true;
      searchAddValue({
        filters: '',
        sorts: '',
        page: this.currentPage,
        size: this.pageSize
      }).then((res) => {
        if (res.data.successFlg === true) {
          this.tableData = res.data.detailModelList;
          this.currentPage = res.data.currPage;
          this.total = res.data.totalCount;
          this.pageSize = res.data.pageSize;
          this.listLoading = false;
        } else {
          this.$message.error(res.data.errorMsg);
        }
      });
    }

  },
  // 初始化数据
  mounted () {
    this.getTableData();
    this.$store.commit('initDict');
  }
};
</script>
<style lang="less"></style>
